import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Main {
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while(true) {
			int k = Integer.parseInt(br.readLine().trim());
			if(k == 0) {
				break;
			}
			String str1 = br.readLine();
			String str2 = br.readLine();
			long t = System.currentTimeMillis();
			Integer[][] mem = new Integer[str1.length()][str2.length()];
			Integer[][] lookup = new Integer[str1.length()][str2.length()];
			
			System.out.println(lcs(str1, str2, str1.length() - 1, str2.length() - 1, k, mem, lookup));
			System.out.println(System.currentTimeMillis() - t);
		}
	}
	
	private static int lcs(String str1, String str2, int i, int j, int k, Integer[][] mem, Integer[][] lookup) {
		
		if(i < 0 || j < 0) {
			return 0;
		}
		if(mem[i][j] != null) {
			return mem[i][j];
		}
		
		if(lookup[i][j] == null) {
			recurse(str1, str2, i, j, lookup);
		} 
				
		if(lookup[i][j] >= k) {
			int val = lookup[i][j] + lcs(str1, str2, i - lookup[i][j], j - lookup[i][j], k, mem, lookup);
			mem[i][j] = val;
			return val;
		} else {
			
			int val = Math.max(lcs(str1, str2, i, j - 1, k, mem, lookup),
					lcs(str1, str2, i - 1, j, k, mem, lookup));
			mem[i][j] = val;
			return val;
		}
		
	}
	
	private static int max(int a, int b, int c) {
		int max = Math.max(a, b);
		return Math.max(max, c);
	}
	
	private static int recurse(String str1, String str2, int i, int j, Integer[][] lookup) {
		if(i < 0 || j < 0) {
			return 0;
		}
		if(lookup[i][j] != null) {
			return lookup[i][j];
		}
		if(str1.charAt(i) == str2.charAt(j)) {
			lookup[i][j] = recurse(str1, str2, i - 1, j - 1, lookup) + 1;
		} else {
			lookup[i][j] = 0;
		}
		return lookup[i][j];
		
	}
	
	

}

